package cn.stronglink.assetmanage.business.check.model;

import android.graphics.Bitmap;
import android.view.Display;

import com.google.gson.Gson;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.BrandDao;
import cn.stronglink.assetmanage.dao.CheckDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.ModelDao;
import cn.stronglink.assetmanage.dao.RoomDao;
import cn.stronglink.assetmanage.dao.TypeDao;
import cn.stronglink.assetmanage.dao.UserDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Model;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.business.check.CheckService;
import okhttp3.Call;
import okhttp3.Response;
//import okhttp3.Callback;
//import okhttp3.Response;

/**
 * 入库验收详情页数据操作
 * Created by chencongcong on 2018/6/15.
 */

public class CheckDetailModel {
    CheckService checkService;
    private Asset syncAsset;
    private onCheckDetailListener checkDetailListener;

    public onCheckDetailListener getCheckDetailListener() {
        return checkDetailListener;
    }

    public void setCheckDetailListener(onCheckDetailListener checkDetailListener) {
        this.checkDetailListener = checkDetailListener;
    }

    public CheckDetailModel() {
        checkService = new CheckService();
    }

    /**
     * 根据资产id获得资产详情
     *
     * @param asset_id
     * @return
     */
    public Asset getAssetById(long asset_id) {
        if (asset_id != 0 ) {
            Asset asset = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().queryBuilder().where(AssetDao.Properties.Asset_id_key.eq(asset_id)).unique();
            if (asset != null) {
                return asset;
            }
        }
        return null;
    }

    public Check getCheckById(String check_id) {
        if (check_id != null && !"".equals(check_id)) {
            Check check = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().queryBuilder().where(CheckDao.Properties.Id_.eq(check_id)).
                    unique();
            if (check != null) {
                return check;
            }
        }
        return null;
    }


    public Bitmap getBitmapByUrl(String url) throws FileNotFoundException {
        return ImageUtils.getIntance().getimage(url);
    }

    /**
     * 保存入库数据
     *
     * @param asset
     */
    public void saveCheckData(Asset asset) {
        syncAsset = asset;
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().insertOrReplace(asset);
    }


    /**
     * 删除同步的数据
     */
    public void deleteSyncData() {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().delete(syncAsset);
    }

    public interface onCheckDetailListener {
        void onFailure(Call call, IOException e);

        void onResponse(Call call, Response response);
    }

    /**
     * 根据地点名称获取该地点的index(id)
     *
     * @param roomName
     * @return
     */
    public long getRoomIndex(String roomName) {
        if (roomName != null && !"".equals(roomName)) {
            List<Room> roomList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().
                    queryBuilder().where(RoomDao.Properties.Name.eq(roomName)).list();
            if (roomList != null && roomList.size() > 0) {
                Room room = roomList.get(0);
                return room.getRoom_id();
            }
        }
        return -1;
    }

    public Room getRoom(String roomName) {
        if (roomName != null && !"".equals(roomName)) {
            List<Room> roomList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().
                    queryBuilder().where(RoomDao.Properties.Name.eq(roomName)).list();
            if (roomList != null && roomList.size() > 0) {
                Room room = roomList.get(0);
                return room;
            }
        }
        return null;
    }

    /**
     * 获取地点列表
     *
     * @return
     */
    public List<Room> getRoomData() {
        List<Room> roomList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (roomList != null) {
            return roomList;
        }
        return null;
    }

    /**
     * 获得位置列表
     *
     * @return
     */
    public String[] getRoomArr() {
        List<Room> roomList = getRoomData();
        if (roomList != null && roomList.size() > 0) {
            String[] roomArr = new String[roomList.size()];
            for (int i = 0; i < roomList.size(); i++) {
                roomArr[i] = roomList.get(i).getName();
            }
            return roomArr;
        }
        return null;
    }

    /**
     * 根据品牌名称获取该品牌的index(id)
     *
     * @param brandName
     * @return
     */
    public long getBrandIndex(String brandName) {
        if (brandName != null && !"".equals(brandName)) {
            Brand brand = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().
                    queryBuilder().where(BrandDao.Properties.Name.eq(brandName)).unique();
            if (brand != null) {
                return brand.getBrand_id();
            }
        }
        return -1;
    }

    /**
     * 根据品牌名称获取该品牌的Id_
     *
     * @param brandName
     * @return
     */
    public String getBrandId(String brandName) {
        if (brandName != null && !"".equals(brandName)) {
            Brand brand = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().
                    queryBuilder().where(BrandDao.Properties.Name.eq(brandName)).unique();
            if (brand != null) {
                return brand.getId_();
            }
        }
        return null;
    }

    public Brand getBrand(String brandName) {
        if (brandName != null && !"".equals(brandName)) {
            Brand brand = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().
                    queryBuilder().where(BrandDao.Properties.Name.eq(brandName)).unique();
            if (brand != null) {
                return brand;
            }
        }
        return null;
    }


    /**
     * 获取品牌列表
     *
     * @return
     */
    public List<Brand> getBrandData() {
        List<Brand> brandList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (brandList != null) {
            return brandList;
        }
        return null;
    }

    /**
     * 获得品牌列表
     *
     * @return
     */
    public String[] getBrandArr() {
        List<Brand> brandList = getBrandData();
        if (brandList != null && brandList.size() > 0) {
            String[] brandArr = new String[brandList.size()];
            for (int i = 0; i < brandList.size(); i++) {
                brandArr[i] = brandList.get(i).getName();
            }
            return brandArr;
        }
        return null;
    }

    /**
     * 根据类型名称获取该类型的index(id)
     *
     * @param typeName
     * @return
     */
    public long getTypeIndex(String typeName) {
        if (typeName != null && !"".equals(typeName)) {
            Type type = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().
                    queryBuilder().where(TypeDao.Properties.Name.eq(typeName)).unique();
            if (type != null) {
                return type.getType_id();
            }
        }
        return -1;
    }


    public Type getType(String typeName) {
        if (typeName != null && !"".equals(typeName)) {
            Type type = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().
                    queryBuilder().where(TypeDao.Properties.Name.eq(typeName)).unique();
            if (type != null) {
                return type;
            }
        }
        return null;
    }

    /**
     * 根据类型名称获取该类型的Id_
     *
     * @param typeName
     * @return
     */
    public String getTypeId(String typeName) {
        if (typeName != null && !"".equals(typeName)) {
            Type type = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().
                    queryBuilder().where(TypeDao.Properties.Name.eq(typeName)).unique();
            if (type != null) {
                return type.getId_();
            }
        }
        return null;
    }


    /**
     * 获取类型列表
     *
     * @return
     */
    public List<Type> getTypeData() {
        List<Type> typeList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (typeList != null) {
            return typeList;
        }
        return null;
    }


    /**
     * 获得资产类别列表
     *
     * @return
     */
    public String[] getAssetTypeArr() {
        List<Type> typeList = getTypeData();
        if (typeList != null && typeList.size() > 0) {
            String[] typeArr = new String[typeList.size()];
            for (int i = 0; i < typeList.size(); i++) {
                typeArr[i] = typeList.get(i).getName();
            }
            return typeArr;
        }
        return null;
    }

    /**
     * 根据用户名称获取该类型的index(id)
     *
     * @param userName
     * @return
     */
    public long getUserIndex(String userName) {
        if (userName != null && !"".equals(userName)) {
            User user = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getUserDao().
                    queryBuilder().where(UserDao.Properties.Name.eq(userName)).unique();
            if (user != null) {
                return user.getUser_id();
            }
        }
        return -1;
    }

    public User getUser(String userName) {
        if (userName != null && !"".equals(userName)) {
            User user = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getUserDao().
                    queryBuilder().where(UserDao.Properties.Name.eq(userName)).unique();
            if (user != null) {
                return user;
            }
        }
        return null;
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    public List<User> getUserData() {
        List<User> userList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getUserDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (userList != null) {
            return userList;
        }
        return null;
    }


    /**
     * 获得用户数据
     *
     * @return
     */
    public String[] getUserArr() {
        List<User> userList = getUserData();
        if (userList != null && userList.size() > 0) {
            String[] userArr = new String[userList.size()];
            for (int i = 0; i < userList.size(); i++) {
                userArr[i] = userList.get(i).getName();
            }
            return userArr;
        }
        return null;
    }

    //设备型号根据type和brand查找
    public List<Model> getModelData(String typeId, String brandId) {
        if (typeId != null && brandId != null && !"".equals(typeId) && !"".equals(brandId)) {
            List<Model> modelList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().
                    queryBuilder().where(ModelDao.Properties.TypeId_.eq(typeId))
                    .where(ModelDao.Properties.BrandId_.eq(brandId)).list();
            if (modelList != null) {
                return modelList;
            }
        }
        return null;
    }


    /**
     * 获得设备型号列表
     *
     * @return
     */
    public String[] getModelArr(String typeId, String brandId) {
        List<Model> modelList = getModelData(typeId, brandId);
        if (modelList != null && modelList.size() > 0) {
            String[] modelArr = new String[modelList.size()];
            for (int i = 0; i < modelList.size(); i++) {
                modelArr[i] = modelList.get(i).getName();
            }
            return modelArr;
        }
        return null;
    }

    public String[] getModelArr() {
        List<Model> modelList = getmodelData();
        if (modelList != null && modelList.size() > 0) {
            String[] modelArr = new String[modelList.size()];
            for (int i = 0; i < modelList.size(); i++) {
                modelArr[i] = modelList.get(i).getName();
            }
            return modelArr;
        }
        return null;
    }

    public List<Model> getmodelData() {
        List<Model> modelList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().
                queryBuilder().list();
        ;
        if (modelList != null && modelList.size() > 0) {
            return modelList;
        }
        return null;
    }

    public long getModelIndex(String typeId, String brandId, String modelName) {
        if (typeId != null && brandId != null && !"".equals(typeId) && !"".equals(brandId)) {
            Model model = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().
                    queryBuilder().where(ModelDao.Properties.TypeId_.eq(typeId))
                    .where(ModelDao.Properties.BrandId_.eq(brandId)).where(ModelDao.Properties.Name.eq(modelName)).unique();
            if (model != null) {
                return model.getModel_id();
            }
        }
        return -1;
    }

    public Model getModel(String typeId, String brandId, String modelName) {
        if (typeId != null && brandId != null && !"".equals(typeId) && !"".equals(brandId)) {
            Model model = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().
                    queryBuilder().where(ModelDao.Properties.TypeId_.eq(typeId))
                    .where(ModelDao.Properties.BrandId_.eq(brandId)).where(ModelDao.Properties.Name.eq(modelName)).unique();
            if (model != null) {
                return model;
            }
        }
        return null;
    }


}
